package com.xxgc.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxgc.springboot.controller.exception.custom.NotQuartzClassFoundException;
import com.xxgc.springboot.controller.exception.custom.QuartzTimeException;
import com.xxgc.springboot.entity.TaskInfo;
import com.xxgc.springboot.result.Result;
import com.xxgc.springboot.service.ITaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 定时任务表 前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2022-09-20
 */
@RestController
@Api(tags = "定时任务表相关接口")
@RequestMapping("/task-info")
//必须是某个角色时才能请求
@RequiresRoles("admin")
public class TaskInfoController {

    @Resource
    private ITaskInfoService taskInfoService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperation(value = "定时任务表查询所有接口", notes = "查询所有接口")
    @GetMapping
    public Result<List<TaskInfo>> findAll() {
        return Result.ok(taskInfoService.list());
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperation(value = "定时任务表查更具id查询", notes = "根据id查询数据接口")
    @GetMapping("/{id}")
        public Result<TaskInfo> findOne(@PathVariable Integer id) {
        return Result.ok(taskInfoService.getById(id));
    }

    /**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "定时任务表分页查询接口", notes = "分页查询接口")
    @GetMapping("/page")
    public Result<Page<TaskInfo>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        QueryWrapper<TaskInfo> queryWrapper = new QueryWrapper<>();
        return Result.ok(taskInfoService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 新增
     * @param
     * @return
     */
    @ApiOperation(value = "定时任务表新增", notes = "新增")
    @PostMapping
    public Result save(@RequestBody TaskInfo taskInfo){
        try {
            taskInfoService.saveOrTask(taskInfo);
        }catch (ClassNotFoundException e){
            //抛自己的自定义异常
            throw new NotQuartzClassFoundException("SpringBoot",taskInfo);
        }catch (SchedulerException e){
            throw new QuartzTimeException("SpringBoot",taskInfo);
        }
        return Result.ok();
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperation(value = "定时任务表删除接口", notes = "删除接口")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws SchedulerException {
        taskInfoService.removeTaskById(id);
        return Result.ok();
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @ApiOperation(value = "定时任务表批量删除接口", notes = "批量删除接口")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        taskInfoService.removeByIds(ids);
        return Result.ok();
    }



    /**
     * 暂停接口
     * @param id
     * @return
     */
    @ApiOperation(value = "暂停任务接口", notes = "暂停接口")
    @GetMapping("/suspendTask/{id}")
    public Result suspendTask(@PathVariable Integer id) throws SchedulerException {
        taskInfoService.suspendTaskById(id);
        return Result.ok();
    }

    /**
     * 运行接口
     * @param id
     * @return
     */
    @ApiOperation(value = "开始任务接口", notes = "开始接口")
    @GetMapping("/runTask/{id}")
    public Result runTask(@PathVariable Integer id) throws SchedulerException {
        taskInfoService.runTaskById(id);
        return Result.ok();
    }
}